package com.xcalendar.managementImpl;

import com.xcalendar.database.dao.UserDAO;
import com.xcalendar.database.table.User;
import com.xcalendar.exception.IncorrectNameOrPasswordException;
import com.xcalendar.exception.NameUsedException;
import com.xcalendar.management.UserManager;

public class UserManagerImpl implements UserManager {

	@Override
	public User login(String userName, String password)
			throws IncorrectNameOrPasswordException {
		
		User userLogin = new User();
		userLogin.setUserName(userName);
		userLogin.setPassword(password);
		UserDAO userDAO = new UserDAO();
		User testUser = userDAO.readUser(userLogin);
		if(testUser == null){
			throw new IncorrectNameOrPasswordException("invalid username");
		}
		else{
			if(testUser.getPassword().equals(userLogin.getPassword())){
				return testUser;
			}
			else{
				throw new IncorrectNameOrPasswordException("invalid password");
			}
		}
		
	}

	@Override
	public void createAccount(String userName, String password)
			throws NameUsedException {
		// TODO Auto-generated method stub

			User newUser = new User();
			newUser.setUserName(userName);
			newUser.setPassword(password);
			UserDAO userDAO = new UserDAO();
			
			
			
			
			User testUser = userDAO.readUser(newUser);
			if(testUser == null){
				userDAO.addUser(newUser);
			}
			else{
				throw new NameUsedException("the username " + userName + " is not available");
			}
		
	}

	@Override
	public User updateUser(User user) {
		UserDAO userDAO = new UserDAO();
		//userDAO.updateUser(user);
		return user;
	}

}
